package com.example.app.components

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.*
import com.example.app.R

@OptIn(ExperimentalUnitApi::class)
@Composable
fun TextSample() {
    val anStr = buildAnnotatedString {
        append("点击登录即代表您已同意")
        // pushStringAnnotation push到pop方法,点击用户协议生效
        pushStringAnnotation("protocol", "https://baidu.com")
        withStyle(style = SpanStyle(Color.Blue, textDecoration = TextDecoration.Underline)) {
            append("用户协议")
        }
        pop()
        append("和")
        withStyle(style = SpanStyle(Color.Red, textDecoration = TextDecoration.Underline)) {
            append("各种协议")
        }
    }
    ClickableText(text = anStr, onClick = {
        // it.item代表pushStringAnnotation函数中的annotation
        // getStringAnnotations获取名称为protocol的注解
        // 在定义pushStringAnnotation和pop方法之间触发
        anStr.getStringAnnotations("protocol", start = it, end = it).firstOrNull() ?.let {
            Log.d("=====", "点击到了${it.item}")
        }
        Log.d("-----","文本点击: $it")
    })
//    SelectionContainer() {
//        Text(text = "文本长按选择")
//    }
//    Text(
//        text = "合并使用",
//        textDecoration = TextDecoration.combine(
//            listOf(
//                TextDecoration.Underline,
//                TextDecoration.LineThrough
//            )
//        ),
//        textAlign = TextAlign.Center
//    )
//    Text(
//        text = "wsh3038971",
//        color = Color.Cyan,
//        fontSize = 30f.em,
//        fontWeight = FontWeight.Bold,
//        letterSpacing = 60.sp
//    )
//    Text(
//        text = "删除线",
//        textDecoration = TextDecoration.LineThrough
//    )
//    Text(
//        text = stringResource(id = R.string.test_text),
//        color = Color(255, 255, 0)
//    )
}

@Preview(showSystemUi = true)
@Composable
fun TextSamplePreview() {
    TextSample()
}